package com.dy.video.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.dy.video.service.Transfer;

import lib.BasicService;

public class TransferImpl extends BasicService implements Transfer{
	private static String ffmpegpath = "lib/ffmpeg.exe";
	@Override
	public boolean transferVideo(String path,String targetExtension,String targetfolder) {
		/**
		 * -ar 设定采样率
		 */
		String filerealname  = StringUtils.substringBefore(StringUtils.substringAfterLast(path, "\\"), ".");
        Process p = null;
		List<String> commend = new ArrayList<>();  
        commend.add(ffmpegpath);  
        commend.add("-i");
        commend.add(path);
        commend.add("-ab"); 
        commend.add("64");
        commend.add("-ac");
        commend.add("2");
        commend.add("-ar");
        commend.add("22050");
        commend.add("-qscale"); 
        commend.add("6");
        commend.add(targetfolder + filerealname + targetExtension);
        try {
            ProcessBuilder builder = new ProcessBuilder();  
            builder.command(commend);
            builder.redirectErrorStream(true);
            p = builder.start();
            System.out.println("开始转码...");
            long now = System.currentTimeMillis();
            System.out.println("转码完成，时间："+(System.currentTimeMillis()-now));
            return true; 
        } catch (Exception e) {  
            e.printStackTrace();  
            return false;  
        }finally{
        	try {  
                if(p != null){
                	p.destroy();
                }
            } catch (Exception e) {  
            	e.printStackTrace();
            }  
        }
	}
}
